﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RVEAd.Business;
using System.Configuration;
using RVEAd.Business.Model;

namespace RVEAd.LiveClient.Controls
{
    public partial class IntervalGrid : UserControl
    {
        #region private fields

        private RveBL rveBL;
        private string decititToken = string.Empty;
        private List<AdPlanModel> data;
        
        #endregion

        #region properties

        [BrowsableAttribute(false)]
        public string IntervalName
        {
            set
            {
                label1.Text = value;
            }
        }

        [BrowsableAttribute(false)]
        public DateTime Date { get; set; }

        [BrowsableAttribute(false)]
        public IntervalEnum IntervalType { get; set; }

        #endregion

        #region ctor

        public IntervalGrid()
        {
            InitializeComponent();
            this.Load += new EventHandler(IntervalGrid_Load);
            rveBL = new RveBL();
        }

        #endregion

        void IntervalGrid_Load(object sender, EventArgs e)
        {
            this.dataGridViewAM1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridViewAM1_CellPainting);
        }

        void dataGridViewAM1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.Value.ToString().Equals(decititToken) && dataGridViewAM1.Rows.Count > 0)
            {
                dataGridViewAM1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.GreenYellow;
            }
        }

        #region public methods

        public void LoadData()
        {
            decititToken = ConfigurationManager.AppSettings["DeCititToken"];
            data = rveBL.GetAdPlans(Date);

            var intervalDeCitit = GetDeCitit();
            var intervalData = GetData();

            dataGridViewAM1.AutoGenerateColumns = false;
            dataGridViewAM1.DataSource = intervalDeCitit.Union(intervalData).ToList();
            if (dataGridViewAM1.Rows.Count > 0 && dataGridViewAM1.Rows[0] != null)
                dataGridViewAM1.Rows[0].Selected = false;
        }
        
        #endregion

        #region private methods

        private IEnumerable<AdPlanModel> GetData()
        {
            List<AdPlanModel> list = new List<AdPlanModel>();

            switch (IntervalType)
            {
                case IntervalEnum.AM1:
                    list = data.Where(p => p.AM1.Equals("True")).ToList();
                    break;
                case IntervalEnum.AM2:
                    list = data.Where(p => p.AM2.Equals("True")).ToList();
                    break;
                case IntervalEnum.AM3:
                    list = data.Where(p => p.AM3.Equals("True")).ToList();
                    break;
                case IntervalEnum.PM1:
                    list = data.Where(p => p.PM1.Equals("True")).ToList();
                    break;
                case IntervalEnum.PM2:
                    list = data.Where(p => p.PM2.Equals("True")).ToList();
                    break;
            }

            return list;
        }

        private IEnumerable<AdPlanModel> GetDeCitit()
        {
            List<AdPlanModel> list = new List<AdPlanModel>();

            switch (IntervalType)
            {
                case IntervalEnum.AM1:
                    list = data.Where(p => p.AM1.Equals("True") && p.SpotName.Equals(decititToken)).ToList();
                    break;
                case IntervalEnum.AM2:
                    list = data.Where(p => p.AM2.Equals("True") && p.SpotName.Equals(decititToken)).ToList();
                    break;
                case IntervalEnum.AM3:
                    list = data.Where(p => p.AM3.Equals("True") && p.SpotName.Equals(decititToken)).ToList();
                    break;
                case IntervalEnum.PM1:
                    list = data.Where(p => p.PM1.Equals("True") && p.SpotName.Equals(decititToken)).ToList();
                    break;
                case IntervalEnum.PM2:
                    list = data.Where(p => p.PM2.Equals("True") && p.SpotName.Equals(decititToken)).ToList();
                    break;
            }

            return list;
        } 
        
        #endregion
    }
}
